package com.bw.rbac.controller;


import com.bw.rbac.entity.Result;
import com.bw.rbac.entity.User;
import com.bw.rbac.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 青山
 * @since 2022-05-26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 注册
     * @return
     */
    @RequestMapping("/register")
    public Result<String> register(@RequestBody User user) {
        boolean b = userService.save(user);
        if(b){
            return new Result<>(200,"","注册成功");
        }else{
            return new Result<>(400,"注册失败","注册失败");
        }
    }


    @RequestMapping("foretPassword")
    public String foretPassword(){
        return "foretPassword";
    }

    @RequestMapping("loginOk")
    public Result loginOk(){


        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails){// 验证 principal 是不是 UserDetails类型
            return  new Result(200,"登录成功",principal);
        }else{
            return  new Result(201,"登录失败","");
        }
    }

    @RequestMapping("loginfailed")
    public Result loginFailed(){
        //
        return  new Result(202,"用户密码错误","");
    }
    @RequestMapping("logoutok")
    public Result logoutok(){
        //
        return  new Result(200,"登出成功","已经登出lll");
    }




    @RequestMapping("noLogin")
    public ResponseEntity noLogin(){
        //
        return new ResponseEntity(new Result(300,"用户尚未登录",""), HttpStatus.FORBIDDEN);
    }

}
